﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using Microsoft.ApplicationBlocks.Data;
using Word;
//using Word = Microsoft.Office.Interop.Word;//2007
using System.Drawing;
using System.Collections;

namespace kxjsj.FuncClass
{
    class BakClass
    {
        public static void insert_imgNew(OleDbConnection con, string dbname, string fname, string p_id)
        {
            Hashtable id_name = new Hashtable();
            OleDbDataReader r = SqlHelper.ExecuteReader(con, CommandType.Text, "select * from " + dbname + " where p_inner_id='" + p_id + "' order by [order];");
            Word.Application app = new Word.Application();
            Word.Document doc = null;
            object fn = System.Windows.Forms.Application.StartupPath + "\\" + p_id + "\\" + fname;
            object m = System.Reflection.Missing.Value;
            doc = app.Documents.Open(ref fn, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m);
            int i = 2;
            object numrows = 1;
            object f = false; object t = true;
            while (r.Read())
            {
                if (i > 2)
                {
                    //插入行
                    doc.Tables.Item(1).Rows.Item(i).Select();
                    app.Selection.InsertRowsAbove(ref numrows);
                }
                doc.Tables.Item(1).Rows.Item(i).Cells.Item(1).Range.Text = r["order"].ToString();
                doc.Tables.Item(1).Rows.Item(i).Cells.Item(2).Range.Text = r["name"].ToString();
                id_name.Add(r["order"].ToString(), r["name"].ToString());
                i++;
            }
            Word.Range rg = doc.Bookmarks.Item(ref numrows).Range;
            object pagebreak = WdBreakType.wdPageBreak;
            object wdunit = WdUnits.wdCharacter;
            object cnt = 1; object wdlbl = "图表";
            object wdpab = WdCaptionPosition.wdCaptionPositionAbove;
            object titleautotext = "";
            for (int img_odr = id_name.Count; img_odr > 0; img_odr--)
            {
                string iodr = img_odr.ToString();
                if (iodr.Length == 1)
                {
                    iodr = "0" + iodr;
                }
                rg.InlineShapes.AddPicture(System.Windows.Forms.Application.StartupPath + "\\" + p_id + "\\image\\img" + iodr + ".jpg", ref f, ref t, ref m);
                rg.InsertBefore("\n" + iodr + "、" + id_name[iodr].ToString() + "\n");
            }

            doc.SaveAs(ref fn, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m);
            if (doc != null) doc.Close(ref m, ref m, ref m);
            if (app != null) app.Quit(ref m, ref  m, ref  m);

        }
        public static void insert_imgNew311(OleDbConnection con, string dbname, string fname, string p_id)
        {
            Hashtable id_name = new Hashtable();
            OleDbDataReader r = SqlHelper.ExecuteReader(con, CommandType.Text, "select * from " + dbname + " where p_inner_id='" + p_id + "' order by [order];");
            Word.Application app = new Word.Application();
            Word.Document doc = null;
            object fn = System.Windows.Forms.Application.StartupPath + "\\" + p_id + "\\" + fname;
            object m = System.Reflection.Missing.Value;
            doc = app.Documents.Open(ref fn, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m);
            string i_name = System.Windows.Forms.Application.StartupPath + "\\"
                + p_id + "\\image\\img"
                + r["order"].ToString()
                + ".jpg";
            Image img_tmp = Image.FromFile(i_name);

            doc.Activate();
            if (img_tmp.Width > img_tmp.Height)
            {
                app.Selection.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
            }
            app.Selection.PageSetup.BottomMargin = 50;
            app.Selection.PageSetup.TopMargin = 80;
            app.Selection.InsertAfter(r["order"].ToString() + "、" + r["name"].ToString() + "\r\n");

            object endkeytype = WdUnits.wdStory;
            object del_char_type = WdUnits.wdCharacter;
            object f = false; object t = true;
            object pagebreak = WdBreakType.wdSectionBreakNextPage;

            app.Selection.EndKey(ref endkeytype, ref m);
            app.Selection.InlineShapes.AddPicture(i_name, ref f, ref t, ref m); app.Selection.InsertAfter("\r\n");
            doc.Save();
            doc.Close(ref m, ref m, ref m);

            doc.Activate(); doc.Select();
            app.Selection.EndKey(ref endkeytype, ref m);
            if (img_tmp.Width > img_tmp.Height)
            {
                app.Selection.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
            }

            string wd_page_name = System.Windows.Forms.Application.StartupPath + p_id + r["order"].ToString() + ".doc";


            app.Selection.InsertFile(wd_page_name, ref m, ref m, ref m, ref m);
            app.Selection.PageSetup.BottomMargin = 2;
            app.Selection.PageSetup.TopMargin = 30;
            app.Selection.InsertBreak(ref pagebreak);
            app.Selection.EndKey(ref endkeytype, ref m);
            app.Selection.PageSetup.Orientation = WdOrientation.wdOrientPortrait;
            //w = img_tmp.Width; h = img_tmp.Height;
            img_tmp.Dispose();
        }
        public static void insert_img(OleDbConnection con, string dbname, string fname, string p_id)
        {
            Hashtable id_name = new Hashtable();
            OleDbDataReader r = SqlHelper.ExecuteReader(con, CommandType.Text, "select * from " + dbname + " where p_inner_id='" + p_id + "' order by [order];");
            Word.Application app = new Word.Application();
            Word.Document doc = null;
            object fn = System.Windows.Forms.Application.StartupPath + "\\" + p_id + "\\" + fname;
            object m = System.Reflection.Missing.Value;
            //doc = app.Documents.Open(ref fn, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m);
            doc = app.Documents.Open(ref fn, ref m, ref  m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m);
            int i = 2;
            object numrows = 1;
            object f = false; object t = true;
            while (r.Read())
            {
                if (i > 2)
                {
                    //插入行
                    //****************************************//
                    //                    doc.Tables.Item(1).Rows.Item(1).Select();
                    app.Selection.InsertRowsAbove(ref numrows);
                }
                //****************************************//
                //doc.Tables.Item(1).Rows.Item(1).Cells.Item(1).Range.Text = r["order"].ToString();
                //doc.Tables.Item(1).Rows.Item(1).Cells.Item(2).Range.Text = r["name"].ToString();
                id_name.Add(r["order"].ToString(), r["name"].ToString());
                i++;
            }
            Word.Range rg = //doc.Bookmarks.get_Item(ref numrows).Range;//!!!!
                doc.Bookmarks.Item(ref numrows).Range;
            //!!!!!!!!!!!bynlt
            object pagebreak = Word.WdBreakType.wdPageBreak;
            //!!!!!!!!!!!bynlt
            object wdunit = Word.WdUnits.wdCharacter;
            object cnt = 1; object wdlbl = "图表";
            //!!!!!!!!!!!bynlt+word.
            object wdpab = Word.WdCaptionPosition.wdCaptionPositionAbove;
            object titleautotext = "";
            for (int img_odr = id_name.Count; img_odr > 0; img_odr--)
            {
                string iodr = img_odr.ToString();
                if (iodr.Length == 1)
                {
                    iodr = "0" + iodr;
                }
                rg.InlineShapes.AddPicture(System.Windows.Forms.Application.StartupPath + "\\" + p_id + "\\image\\img" + iodr + ".jpg", ref f, ref t, ref m);
                rg.InsertBefore("\n" + iodr + "、" + id_name[iodr].ToString() + "\n");
            }

            doc.SaveAs(ref fn, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m);
            //doc.SaveAs(ref fn, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m);
            if (doc != null) doc.Close(ref m, ref m, ref m);
            if (app != null) app.Quit(ref m, ref  m, ref  m);

        }
    }
}
